package org.hxyjs.dao.impl;

import org.hxyjs.dao.ExpertActivityRecordDao;
import org.hxyjs.entity.ExpertActivityRecord;
import org.hxyjs.mapper.ExpertActivityRecordMapper;
import org.hxyjs.page.PageInfo;
import org.hxyjs.utils.GenNumTools;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Repository("expertActivityRecordDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class ExpertActivityRecordDaoImpl implements ExpertActivityRecordDao {
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private ExpertActivityRecordMapper expertActivityRecordMapper;

    //查询全部
    public List<ExpertActivityRecord> getAllExpertActivityRecords() throws Exception {
        return expertActivityRecordMapper.selectByExample(null);
    }

    //按主键查询
    @Override
    public ExpertActivityRecord getExpertActivityRecordByExarno(String exarno) throws Exception {
        return expertActivityRecordMapper.selectByPrimaryKey(exarno);
    }

    //分页查询
    @Override
    public List<ExpertActivityRecord> getExpertActivityRecordByPage(PageInfo pageInfo) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("currPage", pageInfo.getCurrPage());
        map.put("pageSize", pageInfo.getPageSize());
        int itemCount = (int) expertActivityRecordMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return expertActivityRecordMapper.getExpertActivityRecordByPage(map);
    }

    //删除
    @Override
    public Integer deleteExpertActivityRecordByExarno(String exarno) {
        return expertActivityRecordMapper.deleteByPrimaryKey(exarno);
    }

    //修改
    @Override
    public Integer updateExpertActivityRecordByExarno(ExpertActivityRecord expertActivityRecord) {
        return expertActivityRecordMapper.updateByPrimaryKey(expertActivityRecord);
    }


    //增加
    @Override
    public Integer addExpertActivityRecord(ExpertActivityRecord expertActivityRecord) {
        expertActivityRecord.setExarno(initId());
        return expertActivityRecordMapper.insertSelective(expertActivityRecord);
    }

    public String initId() {
        String maxExarno = expertActivityRecordMapper.getMaxExarno();
        maxExarno = Optional.ofNullable(maxExarno).orElse("0000");
        return GenNumTools.initId("ex", 4, maxExarno);
    }

}